package com.sanshow.charging.util;

import android.util.Log;

import com.sanshow.charging.MyApplication;
import com.sanshow.charging.db.Charging;
import com.sanshow.charging.db.ChargingDao;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtil {

    public static void sendDeviceLoginHttpRequest(String userName, String password, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("u", userName)
                .add("p", password)
                .add("d", DeviceUtil.getSerialNumber())
                .add("k", ConstUtil.deviceKey)
                .build();
        Request request = new Request.Builder().url(ConstUtil.loginUrl).post(formBody).build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendAppUpgradeInfoHttpRequest(okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("k", ConstUtil.deviceKey)
                .add("d", DeviceUtil.getSerialNumber())
                .build();
        Request request = new Request.Builder().url(ConstUtil.upgradeUrl).post(formBody).build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendDeviceInfoHttpRequest(okhttp3.Callback callback) {
        Log.d("tianmin","sn:" + DeviceUtil.getSerialNumber());
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("k", ConstUtil.deviceKey)
                .add("d", DeviceUtil.getSerialNumber())
                .build();
        Request request = new Request.Builder().url(ConstUtil.deviceUrl).post(formBody).build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendAdMediaHttpRequest(int bid, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("b", String.valueOf(bid))
                .add("k", ConstUtil.deviceKey)
                .build();
        Request request = new Request.Builder().url(ConstUtil.adMediaUrl).post(formBody).build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendUpdateItems(okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();

        ChargingDao chargingDao = MyApplication.getDaoInstant().getChargingDao();
        List<Charging> chargingList = chargingDao.queryBuilder().list();
        List<String> charingItems = new ArrayList<>();
        for (Charging charging : chargingList) {
            charingItems.add(charging.getItemCode() + "|" + charging.getInfoCode());
        }
        String strItems = StringUtil.listToString2(charingItems, ',');
        RequestBody formBody = new FormBody.Builder()
                .add("k", ConstUtil.deviceKey)
                .add("d", DeviceUtil.getSerialNumber())
                .add("i", strItems)
                .build();
        Request request = new Request.Builder().url(ConstUtil.updateDeviceItem).post(formBody).build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendUploadOpenLogHttpRequest(String fileName, okhttp3.Callback callback) {
        File file = new File(fileName);
        if (file.exists()) {
            OkHttpClient client = new OkHttpClient();

            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("file", file.getName(),
                            RequestBody.create(MediaType.parse("multipart/form-data"), file))
                    .build();
            Request request = new Request.Builder()
                    .url(ConstUtil.uploadOpenLog)
                    .post(requestBody)
                    .build();
            client.newCall(request).enqueue(callback);
        }
    }


    public static void sendUploadLog(okhttp3.Callback callback,String log) {

        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("content", log)
                .add("deviceid", DeviceUtil.getSerialNumber())
                .build();
        Request request = new Request.Builder().url(ConstUtil.uploadLog).post(formBody).build();
        client.newCall(request).enqueue(callback);
    }
}
